package com.adcok.lcdns.activity.info;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;

import com.adcok.lcdns.util.ApplicationUtils;
import com.adcok.lcdns.R;

/**
 * 기본정보- 세대, 직업 Activity
 * @author Administrator
 *
 */
public class LottoAdInfoGenerationActivity extends Activity {

	private Context mContext;

	/**관심분야 액티비티 요청 인덱스**/
	private final int REQUEST_ACTIVITY_FAVORITE = 0;
	
	/**다음 Button*/
	private Button mNextBtn;

	/**세대(나이)선택 스피너**/
	private Spinner mGenerationSpin;
	/**직업선택 라디오 버튼*/
	private RadioGroup mRadioJobs;
	
	/**세대(나이)선택 시퀀스**/
	private int mGeneration = 0;
	private String[] mGenerationArr= {
		"연령선택",
		"20대",
	    "30대",
	    "40대",
	    "50대",
	    "60대 이상"
	};
	
	/**직업선택 시퀀스**/
	private int mJobsSeq = 0;
	private final int SEQ_JOBS_SELF = 1;
	private final int SEQ_JOBS_WORKER = 2;
	private final int SEQ_JOBS_HOMEMAKER = 3;
	private final int SEQ_JOBS_SUDENT = 4;
	private final int SEQ_JOBS_BUSINESS = 5;
	private final int SEQ_JOBS_PROFESSION = 6;

	/**직업 선택 여부 확인 boolean값**/
	private boolean mIsChecked = false;
	private int mSelectedPostion = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.mContext = this;

		setContentView(R.layout.lottoad_info_generation);

		mGenerationSpin = (Spinner) findViewById(R.id.spinner_generation);
		mRadioJobs = (RadioGroup) findViewById(R.id.radioGroup_jobs);
		mNextBtn = (Button) findViewById(R.id.button_next);

		mGenerationSpin.setAdapter(new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_dropdown_item, mGenerationArr){
			/**
		     * 스피너 클릭시 보여지는 View의 정의
		     */
		    @Override
		    public View getDropDownView(int position, View convertView,
		            ViewGroup parent) {
		 
		        if(position == 0){
		        	TextView tv = new TextView(mContext);
		        	tv.setHeight(0);
		        	tv.setVisibility(View.GONE);
		        	convertView = tv;
		        }
		        else {
		        	convertView = super.getDropDownView(position, null, parent);
		        	TextView tv = (TextView) convertView.findViewById(android.R.id.text1);
			        tv.setText(mGenerationArr[position]);
			        tv.setTextSize(25);
			        tv.setPaintFlags(tv.getPaintFlags() | Paint.FAKE_BOLD_TEXT_FLAG);
			        mSelectedPostion = position;
		        }
		        return convertView;
		    }
		 
		    /**
		     * 기본 스피너 View 정의
		     */
		    @Override
		    public View getView(int position, View convertView, ViewGroup parent) {
		        if (convertView == null) {
		            LayoutInflater inflater = LayoutInflater.from(mContext);
		            convertView = inflater.inflate(
		                    android.R.layout.simple_spinner_item, parent, false);
		        }
		 
		        TextView tv = (TextView) convertView
		                .findViewById(android.R.id.text1);
		        tv.setText(mGenerationArr[position]);
		        tv.setTextSize(14);
		        tv.setPaintFlags(tv.getPaintFlags() | Paint.FAKE_BOLD_TEXT_FLAG);
		        return convertView;
		    }
		});

		mGenerationSpin.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				if(position != 0){
				String item = parent.getItemAtPosition(position).toString();
				mGeneration = Integer.parseInt(item.substring(0, 2));
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});
		
		mGenerationSpin.setSelection(AdapterView.INVALID_POSITION);

		mRadioJobs.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			
					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						mIsChecked = true;
						switch (checkedId) {
						case R.id.radio_job_self:
							Log.d("setOnCheckedChangeListener", getString(R.string.job_self));
							mJobsSeq = SEQ_JOBS_SELF;
							break;
						case R.id.radio_job_worker:
							Log.d("setOnCheckedChangeListener", getString(R.string.job_worker));
							mJobsSeq = SEQ_JOBS_WORKER;
							break;
						case R.id.radio_job_homemaker:
							Log.d("setOnCheckedChangeListener", getString(R.string.job_homemaker));
							mJobsSeq = SEQ_JOBS_HOMEMAKER;
							break;
						case R.id.radio_job_student:
							Log.d("setOnCheckedChangeListener", getString(R.string.job_student));
							mJobsSeq = SEQ_JOBS_SUDENT;
							break;
						case R.id.radio_job_business:
							Log.d("setOnCheckedChangeListener", getString(R.string.job_business));
							mJobsSeq = SEQ_JOBS_BUSINESS;
							break;
						case R.id.radio_job_profession:
							Log.d("setOnCheckedChangeListener", getString(R.string.job_profession));
							mJobsSeq = SEQ_JOBS_PROFESSION;
							break;
						}
					}
				});

		mNextBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mIsChecked && mGeneration > 0) {
					Intent intent = new Intent(mContext, LottoAdInfoFavoriteActivity.class);
					startActivityForResult(intent, REQUEST_ACTIVITY_FAVORITE);
				} else {
					ApplicationUtils.showOkDialog(mContext, 0, getString(R.string.popup_alert), getString(R.string.generation_doNotSelected));
				}
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		if(requestCode == REQUEST_ACTIVITY_FAVORITE && resultCode == RESULT_OK){
			String favorite = data.getStringExtra("favorite");
			Intent intent = new Intent();
			intent.putExtra("favorite", favorite);
			intent.putExtra("generation", mGeneration);
			intent.putExtra("jobs", mJobsSeq);
			setResult(RESULT_OK, intent);
			finish();
		}
	}
}
